package cc.customer.web.center.saleManage;

import cc.customer.center.WorkDiaklogServices;
import cc.customer.common.domain.QueryRequest;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.utils.Constant;
import cc.customer.common.utils.FileUtils;
import cc.customer.cusDynamic.domain.EntityDiakogProjo;
import cc.customer.myCustomer.damain.CusResource;
import cc.customer.system.domain.MyUser;
import cc.customer.system.service.UserService;
import cc.customer.web.sys.controller.base.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Description: customer
 * Created by yhsh on 2019/11/8 10:49
 * version 2.0
 * 方法说明 电话分析页面
 */
@Controller
public class WorkAnlynisController extends BaseController {

	@Autowired
	private UserService userService;
	@Autowired
	private WorkDiaklogServices workDiaklogServices;

	@RequestMapping(value = "workAnlynis/getWorkDialogAncilyList",method = {RequestMethod.GET})
	@ResponseBody
	public List<EntityDiakogProjo> getMyCustomerManageList(EntityDiakogProjo DiakogProjo){

		List<String> userids = new ArrayList<>();
		String usersids = DiakogProjo.getDiaName();
		if (!StringUtils.isEmpty(usersids)){
			List<String> deptids = new ArrayList<>();
			String users[] = usersids.split(",");
			for (String deptOrUserid:users){
				if (deptOrUserid.contains("jpet")){
					String deptid = deptOrUserid.replace("jpet","");
					deptids.add(deptid);
				}else{ userids.add(deptOrUserid); }
			}
			if (!deptids.isEmpty()){
				String depts = StringUtils.join(deptids,",");
				List<MyUser> myUsers = userService.selectUserByDeptid2User(depts);
				myUsers.forEach(myUser->{userids.add(myUser.getUserUuid());});
			}
			DiakogProjo.setDiaNames(userids);
		}else{
			//如果没有选择人员或者是部门
			return new ArrayList<>();
		}
		return workDiaklogServices.dialogConnectStatic(DiakogProjo);
	}


	/**导出客户信息**/
	@RequestMapping(value = "workAnlynis/downworkDiaklogExcel")
	@ResponseBody
	public ResponseBo downworkDiaklogExcel(EntityDiakogProjo DiakogProjo){

		List<EntityDiakogProjo> downList = new ArrayList<>();
		try {
			List<String> userids = new ArrayList<>();
			String usersids = DiakogProjo.getDiaName();
			if (!StringUtils.isEmpty(usersids)){
				List<String> deptids = new ArrayList<>();
				String users[] = usersids.split(",");
				for (String deptOrUserid:users){
					if (deptOrUserid.contains("jpet")){
						String deptid = deptOrUserid.replace("jpet","");
						deptids.add(deptid);
					}else{ userids.add(deptOrUserid); }
				}
				if (!deptids.isEmpty()){
					String depts = StringUtils.join(deptids,",");
					List<MyUser> myUsers = userService.selectUserByDeptid2User(depts);
					myUsers.forEach(myUser->{userids.add(myUser.getUserUuid());});
				}
				DiakogProjo.setDiaNames(userids);
			}
			downList = workDiaklogServices.dialogConnectStatic(DiakogProjo);
			return FileUtils.createExcelByPOIKit("电话联系表格", downList, EntityDiakogProjo.class);
		} catch (Exception e) {
			return ResponseBo.error("导出Excel失败，请联系网站管理员！");
		}
	}
}
